package com.slkj.shilixiaoyuanapp.ui.main.message;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.entity.MessageContactsGroupEntity;
import com.slkj.shilixiaoyuanapp.entity.contacts.PersonBean;

import java.util.List;

/**
 * Created  on 2018-07-18
 *
 * @author 唐开阔
 * @describe 消息-联系人adaptet
 */
public class MessageContactsAdater extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final int TYPE_GROUP = 1;
    public static final int TYPE_PERSON = 2;
    private Context context;

    public MessageContactsAdater( Context context ,List<MultiItemEntity> data) {
        super(data);
        this.context = context;
        addItemType(TYPE_GROUP, R.layout.item_message_contacts_group);
        addItemType(TYPE_PERSON, R.layout.item_message_contacts_person);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case TYPE_GROUP:
                final MessageContactsGroupEntity groupEntity = (MessageContactsGroupEntity) item;
                helper.setText(R.id.tv_name, groupEntity.getName())
                        .setText(R.id.tv_num_school,"("+groupEntity.getSubItems().size()+"人)")
                        .setImageResource(R.id.img_direction, groupEntity.isExpanded() ? R.drawable.ic_up :  R.drawable.ic_down)
                        .itemView.setOnClickListener(v -> {
                    int pos = helper.getAdapterPosition();
                    if (groupEntity.isExpanded()) {
                        helper.setImageResource(R.id.img_direction, R.drawable.ic_down);
                        collapse(pos);
                    } else {
                        helper.setImageResource(R.id.img_direction, R.drawable.ic_up);
                        expand(pos);
                    }
                });
                break;
            case TYPE_PERSON:
                final PersonBean personBean = (PersonBean) item;
                Glide.with(context)
                        .load(personBean.getHead())
                        .error(R.drawable.ic_man)
                        .placeholder(R.drawable.ic_man)
                        .crossFade()
                        .into((ImageView) helper.getView(R.id.img_head));
                helper.setText(R.id.tv_name, personBean.getName());
                helper.setText(R.id.tv_phone, personBean.getPhone());
                ImageView view = helper.getView(R.id.img_call);
                view.setOnClickListener(v -> {

                    new QMUIDialog.MessageDialogBuilder(context)
//                            .setTitle("标题")
                            .setMessage("确认拨打电话吗？")
                            .addAction("取消", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    dialog.dismiss();
                                }
                            })
                            .addAction(0, "拨打", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + personBean.getPhone()));
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    context.startActivity(intent);
                                    dialog.dismiss();

                                }
                            })
                            .create().show();
                });
                break;
        }
    }
}
